﻿using System;
using System.ServiceModel;
using System.Configuration;
using System.ServiceModel.Description;
using org.zx.ms.client.Util;

namespace MediaSrvConsoleHost {
    #region demo service interface and class
    [ServiceContract]
    public interface IConfigurationSrv { }
    public class ConfigurationSrv : IConfigurationSrv { }
    [ServiceContract]
    public interface IWatchSrv { }
    public class WatchSrv : IWatchSrv { }
    #endregion
    public class Program {

        private Logger _log = ServiceLogManager.getLogger(typeof(Program));
        private ServiceHost _mediaServiceHost = null;
        private ServiceHost _watchServiceHost = null;

        /// <summary>
        /// 
        /// </summary>
        static void Main(string[] args) {
            Program app = null;
            try {
                app = new Program();
                app.startService();
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            } finally {
                if (app != null) app.stopService();
                app = null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void startService() {
            _log.info("---- Start Service ----");
            try {
                _mediaServiceHost = openService(typeof(IConfigurationSrv), 
                    typeof(ConfigurationSrv), 
                    ConfigurationManager.AppSettings["CONFIGURATION_SERVICE_URI"]);
                _watchServiceHost = openService(typeof(IWatchSrv), 
                    typeof(WatchSrv), 
                    ConfigurationManager.AppSettings["WATCH_SERVICE_URI"]);
                Console.WriteLine("----  Press \"quit\" to quit the service ----");
                string inputKey = "";
                while (true) {
                    inputKey = Console.ReadLine();
                    if ("quit".Equals(inputKey)) {
                        return;
                    }
                }
            } catch (Exception ex) {
                _log.error(ex.Message);
            } 
        }
        /// <summary>
        /// 
        /// </summary>
        public void stopService() {
            _log.info("---- Stop Service ----");
            closeService(_mediaServiceHost);
            closeService(_watchServiceHost);
        }
        /// <summary>
        /// 
        /// </summary>
        private ServiceHost openService(Type contractType, Type implType, string serviceUri) {
                ServiceHost serviceHost = new ServiceHost(implType);
                // Add behavior for Services - enable WSDL access
                ServiceMetadataBehavior serviceABehavior = new ServiceMetadataBehavior();
                serviceABehavior.HttpGetEnabled = true;
                serviceABehavior.HttpGetUrl = new Uri(serviceUri);
                serviceHost.Description.Behaviors.Add(serviceABehavior);
                // Add Http Binding
                serviceHost.AddServiceEndpoint(contractType, new BasicHttpBinding(), serviceUri);
                serviceHost.Open();
                _log.info("---- Service " + contractType.Name + " Open ----");
                return serviceHost;
        }
        /// <summary>
        /// 
        /// </summary>
        private void closeService(ServiceHost serviceHost) {
            if (serviceHost == null) return;
            if (serviceHost.State == CommunicationState.Faulted) {
                serviceHost.Abort();
            } else {
                serviceHost.Close();
            }
        }
    }
}